package com.shu.mqtt;

import java.net.URISyntaxException;

import org.fusesource.hawtbuf.Buffer;
import org.fusesource.hawtbuf.UTF8Buffer;
import org.fusesource.mqtt.client.Callback;
import org.fusesource.mqtt.client.CallbackConnection;
import org.fusesource.mqtt.client.Listener;
import org.fusesource.mqtt.client.MQTT;
import org.fusesource.mqtt.client.QoS;
import org.fusesource.mqtt.client.Topic;

public class MqttTest {
    public static void main(String[] args) throws URISyntaxException {
//    	for(int i=0;i<10000;i++){
    		aa();
//    	}
    	  try {
              Thread.sleep(10000000000l);
          } catch (InterruptedException e) {
              e.printStackTrace();
          }
    }
    
    public static void aa() throws URISyntaxException{
        MQTT mqtt = new MQTT();
        mqtt.setHost("tcp://192.168.1.15:1882");
        mqtt.setUserName("admin");
        mqtt.setPassword("public");
        mqtt.setKeepAlive((short)2);
//        mqtt.setClientId("uu000000");
//        mqtt.setDispatchQueue(Dispatch.createQueue("mqtt.test.aaa"));
        final CallbackConnection connection = mqtt.callbackConnection();
        connection.listener(new Listener() {

            public void onDisconnected() {
                System.out.println("listen disConnect");
            }

            public void onConnected() {
                System.out.println("listent connect");
            }

            public void onPublish(UTF8Buffer topic, Buffer payload, Runnable ack) {
                System.out.println("listen publish");
                System.out.println(UTF8Buffer.decode(topic));
                System.out.println(UTF8Buffer.decode(Buffer.utf8(payload)));
                ack.run();
                
                connection.publish(UTF8Buffer.decode(Buffer.utf8(payload)), UTF8Buffer.decode(Buffer.utf8(payload+"你好")).getBytes(),
                        QoS.AT_LEAST_ONCE, false, new Callback<Void>() {
                        public void onSuccess(Void v) {
                        System.out.println("publish success");
                        }
                       
                        public void onFailure(Throwable value) {
                        System.out.println("publish fail");
                        }
                        });
            }

            public void onFailure(Throwable value) {
                System.out.println("listen fail" + value.toString());
            }
        });
        connection.connect(new Callback<Void>() {
            public void onFailure(Throwable value) {
                System.out.println("connect fail");
            }

            // Once we connect..
            public void onSuccess(Void v) {
            	 System.out.println("connect sus=========");
                // Subscribe to a topic
                Topic[] topics = { new Topic("MqttTest", QoS.AT_LEAST_ONCE) };
                connection.subscribe(topics, new Callback<byte[]>() {
                    public void onSuccess(byte[] qoses) {
                        System.out.println("subscribe success");
                        System.out.println(new String(qoses).toString());
                    }

                    public void onFailure(Throwable value) {
                        System.out.println("subscribe fail");
                    }
                });
                

//                 Send a message to a topic
                
                
//                 connection.publish("foo", "Hello".getBytes(),
//                 QoS.AT_LEAST_ONCE, false, new Callback<Void>() {
//                 public void onSuccess(Void v) {
//                 System.out.println("publish success");
//                 }
//                
//                 public void onFailure(Throwable value) {
//                 System.out.println("publish fail");
//                 }
//                 });
            }
        });
      
        // To disconnect..
//        connection.disconnect(new Callback<Void>() {
//            public void onSuccess(Void v) {
//                System.out.println("disconnect success");
//            }
//
//            public void onFailure(Throwable value) {
//                System.out.println("fail");
//            }
//        });
    }
}